package org.mineground.commands.irc;

import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.User;
import org.mineground.Main;
import org.mineground.Utilities;
import org.mineground.handlers.irc.CommandExecutor;
import org.mineground.handlers.irc.UserLevel;
import org.mineground.player.MinegroundPlayer;

public class Online implements CommandExecutor {
    @Override
    public void onCommand(User sender, UserLevel level, String channel, String command, String args[]) {
	if (args.length < 1) {
	    Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Usage:" + Colors.NORMAL + " !online [player]");
	    return;
	}

	Player player = Main.getInstance().getServer().getPlayer(args[0]);

	if (player == null) {
	    Main.getInstance().getIRCHandler().sendMessage(channel, Colors.RED + "* Error: " + Colors.NORMAL + "Invalid player.");
	    return;
	}

	MinegroundPlayer playerStats = Main.getInstance().getPlayer(player);
	long onlineTime = (System.currentTimeMillis() - playerStats.getJoinTick()) / 1000L;
	onlineTime += playerStats.getOnlineSeconds();

	String time = Utilities.formatTime(onlineTime);

	StringBuilder out = new StringBuilder();
	out.append(Colors.DARK_BLUE);
	out.append("* ");
	out.append(Utilities.fixName(player));
	out.append(Colors.DARK_BLUE);
	out.append(" has been online for ");
	out.append(time);
	out.append(".");

	Main.getInstance().getIRCHandler().sendMessage(channel, out.toString());
    }
}
